package com.smartthinking.destinomusical;

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.smartthinking.destinomusical.base.BaseActivity;
import com.smartthinking.destinomusical.db.ControlBD;
import com.smartthinking.destinomusical.obj.Usuario;

public class SuscribirWeb extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.suscribir_web);
		
		setTopBar(0, 0, R.string.boton_comprar);
		WebView webview = (WebView) findViewById(R.id.webView1);

		ControlBD bd = new ControlBD(this);
		Usuario usuario = bd.selectUser();
		String strMac = getMACAddress("eth0");
		
		String strURL = "http://www.k-box.com/Login?mail="+usuario.getMail()+"&pass="+usuario.getPassword()+"&idioma=es&ipCli="+strMac+"&s=s";
		
		WebSettings webSettings = webview.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setBuiltInZoomControls(false);
		webview.setWebViewClient(new WebViewClient());
		webview.loadUrl(strURL);
	}
	
    public static String getMACAddress(String interfaceName) {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                if (interfaceName != null) {
                    if (!intf.getName().equalsIgnoreCase(interfaceName)) continue;
                }
                byte[] mac = intf.getHardwareAddress();
                if (mac==null) return "";
                StringBuilder buf = new StringBuilder();
                for (int idx=0; idx<mac.length; idx++)
                    buf.append(String.format("%02X:", mac[idx]));       
                if (buf.length()>0) buf.deleteCharAt(buf.length()-1);
                return buf.toString();
            }
        } catch (Exception ex) { } // for now eat exceptions
        return "";
    }
}